<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/10
 * Time: 10:02
 */

namespace Admin\Frontend\Controllers;


use Admin\Models\school\XhschoolInfo;
use Admin\Models\user\XhsysUser;
use Helpers\Assets;
use Helpers\Tool;
use Helpers\XhException;
use Modules\School;
use Services\BigData;

class StatsTchController extends ControllerBase
{


    public static $chineseRate = [
        'eightyRate' => '(80%,100%]',
        'sixtyRate' => '(60%,80%]',
        'fortyRate' => '(40%,60%]',
        'twentyRate' => '(20%,40%]',
        'zeroRate' => '(0%,20%]',
        'noUseRate' => '[0%]'
    ];

    /**
     * 云教辅使用统计
     *
     * create by dale  20181010
     */
    public function indexAction()
    {
        $this->view->pick('stats_tch/index');
    }

    /**
     * 教辅申请
     *
     * @param null $type
     *
     * @return \Phalcon\Mvc\View
     *
     * create by dale  20181010
     */
    public function statsTchBookAction($type = null)
    {
        $this->view->pick('stats_tch/statsTchBook');
        Assets::select2();
        Assets::importJs('js/stats_tch/statsTchBook.js');
        return $this->view->setVars([
            'startTime' => date('Y-m').'-01',
            'endTime'   => date('Y-m-d', strtotime('-1 day')),
            'type'      => $type,
            'schools'   => XhschoolInfo::getSchoolSupportArray(true),
        ]);
    }

    /**
     * 获取统计数据
     *
     * @param null $type
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20181010
     */
    public function statsTchDetailAction($type = null)
    {
        try {
            $startTime = $this->request->getQuery('startTime', 'string');
            $endTime = $this->request->getQuery('endTime', 'string');
            if (!$startTime || !$endTime) {
                return $this->sendViewError('缺少开始时间或者结束时间');
            }
            if ($type == 'allSchool') {
                $startTime = $startTime. ' 00:00:00';
                $endTime   = $endTime. ' 23:59:59';
                $data = BigData::getAllSchoolUseRate(strtotime($startTime), strtotime($endTime));
            } elseif ($type == 'schools') {
                $startTime = strtotime($startTime. ' 00:00:00');
                $endTime   = strtotime($endTime. ' 23:59:59');
                $schools = $this->request->getQuery('schools', 'string');
                if (!$schools) {
                    $data = BigData::getSchoolUseRate(null, $startTime, $endTime);
                } else {
                    $schoolIds = explode(',', $schools);
                    if (!in_array(0, $schoolIds)) {
                        $data = BigData::getSchoolUseRate($schools, $startTime, $endTime);
                    } else {
                        $data = BigData::getSchoolUseRate(null, $startTime, $endTime);
                    }
                }
            } elseif ($type == 'teachers') {
                $startTime = strtotime($startTime. ' 00:00:00');
                $endTime   = strtotime($endTime. ' 23:59:59');
                $schoolId  = $this->request->getQuery('schoolId', 'string');
                $teachers  = $this->request->getQuery('teachers', 'string');
                if (!$schoolId) {
                    return $this->sendErrorResponse('缺少学校id');
                }
                if (!$teachers) {
                    $data = BigData::getTchUseRate($schoolId, null, $startTime, $endTime);
                } else {
                    $tchIds = explode(',', $teachers);
                    if (!in_array(0, $tchIds)) {
                        $data = BigData::getTchUseRate($schoolId, $teachers, $startTime, $endTime);
                    } else {
                        $data = BigData::getTchUseRate($schoolId, null, $startTime, $endTime);
                    }
                }
            } else {
                $this->sendViewError('未知type类型');
            }
            if (Tool::getValue($data, 'code', null)) {
                return $this->sendViewError($data['msg']);
            }
            $this->view->setVar('data', $data);
            $this->view->setVars([
                'type' => $type,
                'chineseRate' => self::$chineseRate
            ]);
        } catch (XhException $e) {
            $this->createCommitLog($e->getMessage(), $e->getDescription());
            return $this->sendViewError($e->getMessage());
        }
    }

    /**
     * 获取教师
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20181010
     */
    public function getTeacherAction()
    {
        try {
            $schoolId= $this->request->getPost('schoolId', 'int');
            if (!$schoolId) {
                return $this->sendErrorResponse(400, '缺少学校id');
            }
            $users = XhsysUser::find([
                'columns'    => 'iUserId,sUserName',
                'conditions' => 'bDelete = 0 and iUserType = 4 and bDisable = 0 and bHistory = 0 
                    and iSchoolId = :schoolId: ',
                'bind'       => ['schoolId' => $schoolId]
            ])->toArray();
            $users = array_column($users, 'sUserName', 'iUserId');
            return $this->sendJsonResponse($users);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(
                500,
                $e->getMessage(),
                $e instanceof XhException ? $e->getDescription() : $e->getTrace()
            );
        }
    }


}